from appium import webdriver
from appium.options.common import AppiumOptions
from appium.webdriver.common.appiumby import AppiumBy
# from selenium.webdriver.common.by import By
# from appium.webdriver.common.touch_action import TouchAction

from selenium.webdriver import Keys
from selenium.webdriver.common.actions.action_builder import ActionBuilder

from selenium.webdriver.remote.mobile import Mobile
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait


caps={
    "deviceName":"127.0.0.1:7555",
    "platformName":"Android",
    "platformVersion":"12",
    "packageName":"com.sina.weibo",
    "appActivity":".SplashActivity",
    # "automationName":"UiAutomator2" #apppium2使用时加上此语句
}
url="http://127.0.0.1:4723"
#"""
#appium2版本使用此方法创建
options = AppiumOptions().load_capabilities(caps)
driver = webdriver.Remote(url,options=options)
#"""

driver = webdriver.Remote(url,caps)

# driver.find_element(AppiumBy.XPATH,"")#aapium2.X版本使用MobileBy,但也兼容By
driver.find_element(MobileBy.XPATH,"")
driver.implicitly_wait(10)
WebDriverWait(driver,10).until(EC.presence_of_element_located((AppiumBy.XPATH,'')))
#拖动 appium1.X版本
action = TouchAction(driver) #2.X版本已经取消了该类
action.press(x=12,y=23).wait(200).move_to(x=100,y=200).release().perform()
'''
#拖动 appium2.X版本
action = ActionBuilder(driver)
action.pointer_action.move_to_location(100,50).pointer_down().pointer_move_to_location(23,43).pointer_up().perform()
'''
driver.find_element(AppiumBy.XPATH,"")
driver.press_key(66)#按下回车
driver.swipe(start_x=100,start_y=100,end_x=200,end_y=300,duration=200)#滑动
driver.find_element(AppiumBy.XPATH,"").click()
#还可以使用tap()
action1 = TouchAction(driver)
action1.tap(x=21,y=78)
#点击，本质上仍然是使用上面那个方法
driver.tap([(12,323)])
driver.find_element(AppiumBy.XPATH,"").send_keys(Keys.ENTER)#模拟enter回车
